package com.prp.bo;

import java.io.File;
import java.util.List;

import org.hibernate.Transaction;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.IUserInformationDAO;
import com.prp.db.po.UserInformation;
import com.prp.gwt.client.ClientConstants;

public class User implements IsSerializable{

	public static boolean hasUser(String email) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
//		List result = userDAO.findByEmail(email);
		if (userDAO.findByEmail(email) == null)
			return false;
		return !userDAO.findByEmail(email).isEmpty();
	}

	public static boolean hasUser(int id) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		if(userDAO.findById(id) == null)
			return false;
		return true;
	}
	
	public static boolean isActivated(String email) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
//		List result = userDAO.findByEmail(email);
		return ((UserInformation) userDAO.findByEmail(email).get(0))
				.getActivated();
	}

	public static void createUser(String email, String username, String password) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = new UserInformation();
			user.setEmail(email);
			user.setUserRegName(username);
			user.setPassword(password);
			user.setGender(SNSConstants.User_Male);
			user.setAge(18);
			user.setIdentity(ClientConstants.USER);
			//user.setUserClassId(1);
			//user.setUserGradeId(1);
			user.setActivated(false);
			userDAO.save(user);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}
	
	public static boolean checkPassword(String email, String password) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		UserInformation info = (UserInformation) userDAO.findByEmail(email)
				.get(0);
		if (info == null)
			return false;
		return info.getPassword().equals(password);
	}

	public static UserInformation getUser(String email) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		return (UserInformation) userDAO.findByEmail(email).get(0);
	}

	public static UserInformation getUser(int userId) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		return (UserInformation) userDAO.findById(userId);
	}

	public static void setPortrait(int userId, String path) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = (UserInformation) userDAO.findById(userId);
			user.setPictureRoutine(path);
			userDAO.attachDirty(user);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}

	public static boolean modifyUserInfo(int id, com.prp.gwt.client.vo.User vo) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = (UserInformation) userDAO.findById(id);
			if(user == null)
				return false;
			user.setUserRegName(vo.getUsername());
			user.setAge(vo.getAge());
			user.setUserRealName(vo.getRealname());
			user.setGender(vo.getGender());
			userDAO.attachDirty(user);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.getSession().close();
		}
		return true;
	}

	public static boolean modifyPassword(int id, String password){
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = userDAO.findById(id);
			user.setPassword(password);
			userDAO.attachDirty(user);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}

	public static void deletePortrait(int id, String basePath) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		String path = userDAO.findById(id).getPictureRoutine();
		File file = new File(basePath+path);
		file.delete();
	}
	
	public static com.prp.gwt.client.vo.User parse(UserInformation info){
		com.prp.gwt.client.vo.User user = new com.prp.gwt.client.vo.User();
		user.setEmail(info.getEmail());
		user.setId(info.getIdentity());
		if (info.getPictureRoutine() == null)
			if (info.getGender() == SNSConstants.User_Male)
				user.setPortrait(SNSConstants.MALE_PORTRAIT);
			else
				user.setPortrait(SNSConstants.FEMALE_PORTRAIT);
		else
			user.setPortrait(info.getPictureRoutine());
		user.setId(info.getUserId());
		user.setUsername(info.getUserRegName());
		user.setIdentity(info.getIdentity());
		user.setRealname(info.getUserRealName());
		user.setAge(info.getAge());
		user.setGender(info.getGender());
		user.setBlog(info.getBlog());
		user.setIntroduction(info.getIntroduction());
		user.setAddress(info.getAddress());
		return user;
	}

	public static List getUserList() {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		List userList = userDAO.findAll();
		return userList;
	}
	
	public static boolean modifyRegName(int id, String regName){
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = userDAO.findById(id);
			user.setUserRegName(regName);
			userDAO.attachDirty(user);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}
	
	public static boolean modifyRealName(int id, String realName){
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = userDAO.findById(id);
			user.setUserRealName(realName);
			userDAO.attachDirty(user);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}
	
	public static boolean modifyBlog(int id, String blog){
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = userDAO.findById(id);
			user.setBlog(blog);
			userDAO.attachDirty(user);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}
	
	public static boolean modifyAddress(int id, String address){
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = userDAO.findById(id);
			user.setAddress(address);
			userDAO.attachDirty(user);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}
	
	public static boolean modifyIntroduction(int id, String introduction){
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			UserInformation user = userDAO.findById(id);
			user.setIntroduction(introduction);
			userDAO.attachDirty(user);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}
	
}
